package nccloud.riart.pub.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import nccloud.riart.vo.OAResponseModel;
import nccloud.utils.mobile_riart.MessageLogger;

public class HttpUtils {

	public static List<OAResponseModel> doPost(String url, String json) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		try {
			StringEntity s = new StringEntity(json, "UTF-8");
			s.setContentType("application/json;charset=UTF-8");
			post.setEntity(s);
			HttpResponse res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = res.getEntity();
				InputStream is = entity.getContent();
				BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
				String line = br.readLine();
				StringBuffer sb = new StringBuffer();
				while (line != null) {
					sb.append(line + "\n");
					line = br.readLine();
				}
				is.close();
				String resJson = sb.toString();
				nc.bs.logging.Logger.error("HttpUtils ----- OA返回的json结果:" + resJson);
				nc.bs.logging.Logger.error("HttpUtils ----- OA返回的json结果:" + resJson);
				List<OAResponseModel> resList = JsonUtils.jsonToList(resJson, OAResponseModel.class);
				return resList;
			}
			return null;

		} catch (Exception e) {
			MessageLogger.info("HttpUtils ----- 调用出错【nccloud.riart.pub.utils.HttpUtilson,错误描述：【"+e.getMessage()+"】");
			nc.bs.logging.Logger.error("HttpUtils ----- 调用出错");
			throw new RuntimeException(e);
		}
	}
}
